package com.gelapps.memogel.cards;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.app.Activity;
import android.preference.PreferenceManager;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.gelapps.memogel.Constants;
import com.gelapps.memogel.Functions;
import com.gelapps.memogel.R;
import com.gelapps.memogel.password.FunctionsPass;

public class CardEdit extends Activity {

    private String sKey;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.card_edit);
        String cardName="";
        Bundle bundle = this.getIntent().getExtras();
        if (bundle!=null)
        {
            this.sKey = bundle.getString(Constants.getKeyString());
            cardName = bundle.getString(Constants.getCardsName());
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            if (preferences.getBoolean(Constants.getPrefStringShowPass(), Constants.getPrefDefaultShowPass()))
            {
                ((ImageView) findViewById(R.id.imgViewNumberEdit)).setVisibility(View.GONE);
                ((EditText) findViewById(R.id.editCardNumberEdit)).setInputType(InputType.TYPE_CLASS_NUMBER);
                ((ImageView) findViewById(R.id.imgViewPinEdit)).setVisibility(View.GONE);
                ((EditText) findViewById(R.id.editCardPinEdit)).setInputType(InputType.TYPE_CLASS_NUMBER);
                ((ImageView) findViewById(R.id.imgViewCvvEdit)).setVisibility(View.GONE);
                ((EditText) findViewById(R.id.editCardCVVEdit)).setInputType(InputType.TYPE_CLASS_NUMBER);
            }
        }
        else
            Functions.showLongToast(getApplicationContext(), getString(R.string.str_error_loading_inf));

        Log.i("CardEdit","sKey"+this.sKey);
        if (!cardName.equals(""))
        {
            CardsMaster cToEdit = CardsManager.getInstance().getCard(cardName);
            ((TextView) findViewById(R.id.txtCardNameViewEdit)).setText(cToEdit.getName());
            ((EditText) findViewById(R.id.editCardNumberEdit)).setText(FunctionsPass.decryptString(this.sKey, cToEdit.getNumber()));
            ((EditText) findViewById(R.id.editCardPinEdit)).setText(FunctionsPass.decryptString(this.sKey, cToEdit.getPin()));
            ((EditText) findViewById(R.id.editCardCVVEdit)).setText(FunctionsPass.decryptString(this.sKey, cToEdit.getCVV()));
            ((EditText) findViewById(R.id.editCardPhoneEdit)).setText(cToEdit.getPhoneToBlock());
            ((EditText) findViewById(R.id.editCardCircuitsEdit)).setText(cToEdit.getCircuits());
            ((EditText) findViewById(R.id.editCardExpiryEdit)).setText(cToEdit.getExpiry());
            ((EditText) findViewById(R.id.editCardBalanceEdit)).setText(cToEdit.getBalance());
            ((EditText) findViewById(R.id.editCardHolderEdit)).setText(cToEdit.getHolder());
            ((EditText) findViewById(R.id.editCardBillAddressEdit)).setText(cToEdit.getBillAddress());
            ((EditText) findViewById(R.id.editCardBankEdit)).setText(cToEdit.getBank());
            ((EditText) findViewById(R.id.editCardWebsiteEdit)).setText(cToEdit.getWebSite());
            ((EditText) findViewById(R.id.editCardNoteEdit)).setText(cToEdit.getNote());
            ((Button) findViewById(R.id.btnCardSaveEdit)).setOnClickListener(editCardListener);
        }
        ((ImageView) findViewById(R.id.imgViewNumberEdit)).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view) {
                if (((EditText) findViewById(R.id.editCardNumberEdit)).getInputType()!=InputType.TYPE_CLASS_NUMBER)
                {
                    ((ImageView) findViewById(R.id.imgViewNumberEdit)).setImageDrawable(getResources().getDrawable(R.drawable.secure2));
                    ((EditText) findViewById(R.id.editCardNumberEdit)).setInputType(InputType.TYPE_CLASS_NUMBER);
                }
                else
                {
                    ((ImageView) findViewById(R.id.imgViewNumberEdit)).setImageDrawable(getResources().getDrawable(R.drawable.not_secure2));
                    ((EditText) findViewById(R.id.editCardNumberEdit)).setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
                }
            }
        });
        ((ImageView) findViewById(R.id.imgViewCvvEdit)).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view) {
                if (((EditText) findViewById(R.id.editCardCVVEdit)).getInputType()!=InputType.TYPE_CLASS_NUMBER)
                {
                    ((ImageView) findViewById(R.id.imgViewCvvEdit)).setImageDrawable(getResources().getDrawable(R.drawable.secure2));
                    ((EditText) findViewById(R.id.editCardCVVEdit)).setInputType(InputType.TYPE_CLASS_NUMBER);
                }
                else
                {
                    ((ImageView) findViewById(R.id.imgViewCvvEdit)).setImageDrawable(getResources().getDrawable(R.drawable.not_secure2));
                    ((EditText) findViewById(R.id.editCardCVVEdit)).setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
                }
            }
        });
        ((ImageView) findViewById(R.id.imgViewPinEdit)).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view) {
                if (((EditText) findViewById(R.id.editCardPinEdit)).getInputType()!=InputType.TYPE_CLASS_NUMBER)
                {
                    ((ImageView) findViewById(R.id.imgViewPinEdit)).setImageDrawable(getResources().getDrawable(R.drawable.secure2));
                    ((EditText) findViewById(R.id.editCardPinEdit)).setInputType(InputType.TYPE_CLASS_NUMBER);
                }
                else
                {
                    ((ImageView) findViewById(R.id.imgViewPinEdit)).setImageDrawable(getResources().getDrawable(R.drawable.not_secure2));
                    ((EditText) findViewById(R.id.editCardPinEdit)).setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
                }
            }
        });
    }

    private View.OnClickListener editCardListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String name = ((TextView) findViewById(R.id.txtCardNameViewEdit)).getText().toString(); 
            String number = ((EditText) findViewById(R.id.editCardNumberEdit)).getText().toString();
            String pin = ((EditText) findViewById(R.id.editCardPinEdit)).getText().toString();
            String cvv = ((EditText) findViewById(R.id.editCardCVVEdit)).getText().toString();
            String phone = ((EditText) findViewById(R.id.editCardPhoneEdit)).getText().toString();
            String circuits = ((EditText) findViewById(R.id.editCardCircuitsEdit)).getText().toString();
            String expiry = ((EditText) findViewById(R.id.editCardExpiryEdit)).getText().toString();
            String balance = ((EditText) findViewById(R.id.editCardBalanceEdit)).getText().toString();
            String holder = ((EditText) findViewById(R.id.editCardHolderEdit)).getText().toString();
            String billAddress = ((EditText) findViewById(R.id.editCardBillAddressEdit)).getText().toString();
            String bank =((EditText) findViewById(R.id.editCardBankEdit)).getText().toString() ;
            String webSite= ((EditText) findViewById(R.id.editCardWebsiteEdit)).getText().toString();
            String note = ((EditText) findViewById(R.id.editCardNoteEdit)).getText().toString();
            boolean test = true;
            if (!Functions.isOkCardNumber(number))
            {
                test = false;
                Functions.showShortToast(getBaseContext(), getString(R.string.strCheckCardNumber));
            }
            if (!Functions.isOkCVV(cvv))
            {
                test = false;
                Functions.showShortToast(getBaseContext(), getString(R.string.strCheckCardCvv));
            }
            if (!Functions.isOkPin(pin))
            {
                test = false;
                Functions.showShortToast(getBaseContext(), getString(R.string.strCheckCardPin));
            }
            if (test)
            {
                if (CardsManager.getInstance().modCard(name, number, pin, cvv, phone, circuits,
                        expiry, balance, holder, billAddress, bank, webSite, note, sKey))
                {
                    Functions.showLongToast(getBaseContext(), getString(R.string.strOKCreateFile));
                    setResult(Constants.getResultEdit());
                    finish();
                }
                else
                    Functions.showLongToast(getBaseContext(), getString(R.string.strErrorCreateFile));
            }
        }
    };




    /*@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.card_edit, menu);
        return true;
   */
    
}
